/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.sys.entity.HkSysAdvRate;
import com.thinkgem.jeesite.modules.hk.sys.service.HkSysAdvRateService;

/**
 * 乐看广告概率配置Controller
 * @author 罗天文
 * @version 2018-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/hkSysAdvRate")
public class HkSysAdvRateController extends BaseController {

	@Autowired
	private HkSysAdvRateService hkSysAdvRateService;
	
	@ModelAttribute
	public HkSysAdvRate get(@RequestParam(required=false) String id) {
		HkSysAdvRate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkSysAdvRateService.get(id);
		}
		if (entity == null){
			entity = new HkSysAdvRate();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:hkSysAdvRate:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkSysAdvRate hkSysAdvRate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkSysAdvRate> page = hkSysAdvRateService.findPage(new Page<HkSysAdvRate>(request, response), hkSysAdvRate); 
		model.addAttribute("page", page);
		return "hk/sys/hkSysAdvRateList";
	}

	@RequiresPermissions("sys:hkSysAdvRate:view")
	@RequestMapping(value = "form")
	public String form(HkSysAdvRate hkSysAdvRate, Model model) {
		model.addAttribute("hkSysAdvRate", hkSysAdvRate);
		return "hk/sys/hkSysAdvRateForm";
	}

	@RequiresPermissions("sys:hkSysAdvRate:edit")
	@RequestMapping(value = "save")
	public String save(HkSysAdvRate hkSysAdvRate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkSysAdvRate)){
			return form(hkSysAdvRate, model);
		}
		if(StringUtils.isEmpty(hkSysAdvRate.getId()))
		{
			Page<HkSysAdvRate> page = hkSysAdvRateService.findPage(new Page<HkSysAdvRate>(), hkSysAdvRate);
			if(page.getCount()>0){
				addMessage(redirectAttributes, "只能配置一个概率");
				return form(hkSysAdvRate, model);
			}
		}
		hkSysAdvRateService.save(hkSysAdvRate);
		addMessage(redirectAttributes, "保存乐看广告概率配置成功");
		return "redirect:"+Global.getAdminPath()+"/sys/hkSysAdvRate/?repage";
	}
	
	@RequiresPermissions("sys:hkSysAdvRate:edit")
	@RequestMapping(value = "delete")
	public String delete(HkSysAdvRate hkSysAdvRate, RedirectAttributes redirectAttributes) {
		hkSysAdvRateService.delete(hkSysAdvRate);
		addMessage(redirectAttributes, "删除乐看广告概率配置成功");
		return "redirect:"+Global.getAdminPath()+"/sys/hkSysAdvRate/?repage";
	}

}